package vn.com.shoppie.activity2;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.message.BasicNameValuePair;

import vn.com.shoppie.activity.R;
import vn.com.shoppie.network.WiModelManager;
import vn.com.shoppie.object.BrandAdapter;
import vn.com.shoppie.sobject.ShoppieObject;
import vn.com.shoppie.util.SConfig;
import vn.com.shoppie.util.SUtilXml;
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

public class LayoutQuaTang extends ContentFragment {
	private Context context;
	private String merchId = "";

	private ListView _mLvQuaTang;

	ArrayList<ShoppieObject> _mDataQuaTang;
	BrandAdapter _mAdapterQuaTang;

	ViewGroup root;

	public LayoutQuaTang(Context context, int layoutResourceId) {
		super(context, context.getResources().getString(R.string.title_quatang), layoutResourceId);
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		this.context = activity;
		if (context instanceof BrandActivity2) {
			merchId = ((BrandActivity2) context).merchId;
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		root = (ViewGroup) inflater.inflate(R.layout.activity_2_brand_quatang, null);

		// init view
		_mLvQuaTang = (ListView) root.findViewById(R.id.lisview);

		// init data
		_mDataQuaTang = new ArrayList<ShoppieObject>();
		_mAdapterQuaTang = new BrandAdapter(context, _mDataQuaTang);
		_mLvQuaTang.setAdapter(_mAdapterQuaTang);
		return root;
	}

	@Override
	public void onResume() {
		super.onResume();
		_mDataQuaTang = new ArrayList<ShoppieObject>();
		_mAdapterQuaTang = new BrandAdapter(context, _mDataQuaTang);
		new AsyncTask<String, Integer, Boolean>() {

			@Override
			protected Boolean doInBackground(String... arg0) {
				_mDataQuaTang = loadData(SConfig.URL_BRAND_GIFT);
				return true;
			}

			protected void onPostExecute(Boolean result) {
				refreshUI();
			};
		}.execute("");
	}

	private void refreshUI() {
		_mAdapterQuaTang = new BrandAdapter(context, _mDataQuaTang);
		_mLvQuaTang.setAdapter(_mAdapterQuaTang);
	}

	private ArrayList<ShoppieObject> loadData(String typeLoad) {
		ArrayList<ShoppieObject> data = new ArrayList<ShoppieObject>();
		// load product
		try {
			ArrayList<NameValuePair> dataToSend = new ArrayList<NameValuePair>();
			dataToSend.add(new BasicNameValuePair("merchId", merchId));
			String xml = SUtilXml.getInstance().postXml(context, SConfig.URL_SHOPPIE_HOME + typeLoad, dataToSend);
			// Log.e("xml",xml);
			if (xml.equals(null)) {
				showToast("Can not connect to Server");
			}
			WiModelManager mng = new WiModelManager();
			mng.parse(xml);
			ArrayList<ShoppieObject> result = mng.getResultShoppieObject();
			Log.e("size " + typeLoad + ":", result.size() + "");
			for (ShoppieObject object : result) {
				data.add(object);
			}
			return data;
		} catch (ClientProtocolException e) {
			// e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// e.printStackTrace();
		} catch (IOException e) {
			// e.printStackTrace();
		}
		return data;
	}

	Toast mToast;

	public void showToast(String text) {
		if (mToast == null)
			mToast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
		mToast.cancel();
		mToast.setText(text);
		mToast.show();
	}
}
